package com.bruce.starts.demo.jdk.jdk8;

import cn.hutool.core.bean.BeanUtil;

import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

public class StreamTest {
    public static void main(String[] args) {
        City  cit1 = new City("南昌","A","test");
        City  cit2 = new City("广州","A","test");
        City  cit3 = new City("南昌","A","test2");
        City  cit4 = new City("郑州","A","test2");
        City  cit5 = new City("长沙","A","test3");
        People p1 = new People("burce",12,"test",List.of(cit1,cit2));
        People p2 = new People("burce2",14,"test2",List.of(cit3,cit4));
        People p3 = new People("burce3",13,"test3",List.of(cit5));
        List<People> peopleList = List.of(p1,p2,p3);
        Map<String,People> filterMap = peopleList.stream().collect(Collectors.toMap(p->p.getBarCode(), Function.identity(),(a,b)->a));
        Map<String,List<People>> map =peopleList.stream().flatMap(p->p.getCityList().stream())
                .collect(Collectors.groupingBy(City::getName, Collectors.mapping(city->{
                    String barCode =city.getBarCode();
                    People people = filterMap.get(barCode);
                    People result = new People("11");
                    BeanUtil.copyProperties(people,result);
                    result.getCityList().removeIf(c->!c.getName().equals(city.getName()));
                    return result;
                }, Collectors.toList())));


        map.forEach((k,v)->{
            System.out.println(k);
            System.out.println(v);
            System.out.println(v.size());
        });
    }
}

class People{
    String name;
    int age;
    String barCode;
    List<City> cityList;

    public People(String name) {
        this.name = name;
    }

    public People(String name, int age, String barCode, List<City> cityList) {
        this.name = name;
        this.age = age;
        this.barCode = barCode;
        this.cityList = cityList;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public List<City> getCityList() {
        return cityList;
    }

    public void setCityList(List<City> cityList) {
        this.cityList = cityList;
    }

    public String getBarCode() {
        return barCode;
    }

    public void setBarCode(String barCode) {
        this.barCode = barCode;
    }
}

class City{
    String name;
    String address;
    String barCode;

    public City(String name, String address, String barCode) {
        this.name = name;
        this.address = address;
        this.barCode = barCode;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getBarCode() {
        return barCode;
    }

    public void setBarCode(String barCode) {
        this.barCode = barCode;
    }
}
